/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.impl;

import java.util.List;

import edu.ccut.titan.component.ILogicContext;
import edu.ccut.titan.component.IParameterContext;
import edu.ccut.titan.component.IParameterInfo;

/**
 * {@link ILogicContext} Abstract implementation
 * 
 * @see ILogicContext
 * @author LeslieGu
 * 
 */
public abstract class AbstractTitanContext implements ILogicContext {

	private String id;

	public AbstractTitanContext(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public abstract Object getValue(String key);

	public abstract void setValue(String key, Object value);

	public abstract Object getValue(IParameterInfo parameterInfo);

	public abstract void setValue(IParameterInfo parameterInfo, Object value);

	public IParameterContext createParameterContext(
			List<IParameterInfo> parameterInfos) {

		TitanParameterContext parameterContext = new TitanParameterContext();

		if (parameterInfos != null) {
			for (IParameterInfo parameterInfo : parameterInfos) {
				// TODO: if a SDO type or a Class, create it for using?
				if (IParameterInfo.PASSING_OUT.equals(parameterInfo
						.getPassingType())) {
					parameterContext.addParameter(null, parameterInfo
							.getClassType());
				} else {
					parameterContext.addParameter(this.getValue(parameterInfo),
							parameterInfo.getClassType());
				}
			}
		}

		return parameterContext;
	}

	public void refreshContextFromParameterContext(
			List<IParameterInfo> parameterInfos,
			IParameterContext parameterContext) {

		if (parameterInfos != null && parameterContext != null) {
			int minParameterNumber = Math.min(parameterContext.getParameters()
					.size(), parameterInfos.size());

			for (int i = 0; i < minParameterNumber; ++i) {
				IParameterInfo parameterInfo = parameterInfos.get(i);
				if (IParameterInfo.PASSING_OUT.equals(parameterInfo
						.getPassingType())
						|| IParameterInfo.PASSING_IN_OUT.equals(parameterInfo
								.getPassingType())) {

					this.setValue(parameterInfo, parameterContext.getParameter(
							i).getValue());
				}
			}
		}
	}
}
